package com.symria.server.dao.common;

import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import com.symria.framework.dao.util.TemplateDao;
import com.symria.framework.page.Page;
import com.symria.server.po.IosActivate;
import com.symria.server.po.IosAdConfig;
import com.symria.server.po.IosChannelConfig;
import com.symria.server.po.IosClick;
import com.symria.system.statement.Statement;

public class IosDao extends TemplateDao {
	
	public void searchAdConfig(IosAdConfig config,Page page){
		Statement stms = stmsFactory.createStatement(" from IosAdConfig c where 1=1");
		if(config!=null){
			stms.append("and", "status", "=", config.getStatus());
			stms.appendLike(null, "and", "packName", config.getPackName());
			
		}
		stms.addOrderBy(" c.packId desc");
		templateDao.search(stms, page);
	}
	public void searchChannelConfig(IosChannelConfig config,Page page){
		Statement stms = stmsFactory.createStatement(" from IosChannelConfig c where 1=1 ");
		if(config!=null){
			stms.append("and", "status", "=", config.getStatus());
			stms.appendLike(null, "and", "channelName", config.getChannelName());
		}
		stms.addOrderBy("c.userId desc");
		templateDao.search(stms, page);
	}
	public void searchIosClick(Page page,IosClick click,Date beginDate ,Date endDate){
	    Statement stms = stmsFactory.createStatement(" from IosClick c where 1=1");
	              if(click!=null){
	            	  stms.append("and", "c.appId", "=", click.getAppId());
	            	  stms.append("and", "c.channel", "=", click.getChannel());
	            	  stms.append("and", "c.mac", "=", click.getMac());
	            	  stms.append("and", "c.idfa", "=", click.getIdfa());
	            	  stms.append("and", "c.status", "=", click.getStatus());
	            	  stms.append("and", "c.isSend", "=", click.getIsSend());
	            	  stms.appendBetween("and", "c.createTime", beginDate, endDate);
	              }
	              stms.addOrderBy(" c.id desc");
	    templateDao.search(stms, page);
	}
	public void searchIosActivate(Page page,IosActivate activate,Date beginDate,Date endDate){
	    Statement stms = stmsFactory.createStatement(" from IosActivate c where 1=1");
	              if(activate!=null){
	            	  stms.append("and", "c.appId", "=", activate.getAppId());
	            	  stms.append("and", "c.channel", "=", activate.getChannel());
	            	  stms.append("and", "c.mac", "=", activate.getMac());
	            	  stms.append("and", "c.idfa", "=", activate.getIdfa());
	            	  stms.append("and", "c.status", "=", activate.getStatus());
	            	  stms.appendBetween("and", "c.createTime", beginDate, endDate);
	              }
	    stms.addOrderBy(" c.id desc");
	    templateDao.search(stms, page);
	}
	public List<IosAdConfig> queryAdConfig(){
		Statement stms = stmsFactory.createStatement(" from IosAdConfig c where 1=1");
		          stms.addOrderBy("c.packId desc");
		return templateDao.query(stms);
	}
	public List<IosChannelConfig> queryChannelConfig(){
		Statement stms = stmsFactory.createStatement(" from IosChannelConfig c where 1=1 ");
		          stms.addOrderBy("c.userId desc");
		          return templateDao.query(stms);
	}
	
	//0:appid 1:channel 2:date 3:total 4:activate 5:send
	public void searchIosDaily(Page page,String appId,String channel,Date beginDate,Date endDate){
		Statement stms = stmsFactory.createNativeStatement("select c.APP_ID appid,c.CHANNEL channel,DATE_FORMAT(c.CREATE_TIME,'%Y-%m-%d') cdate,count(1),sum(if(c.`STATUS`='1',1,0)),sum(if(c.`STATUS`='1'&&c.`IS_SEND`='success',1,0)) ");
		          stms.append(" from `IOS_CLICK` c where 1=1");  
		          stms.appendBetween("AND", "c.CREATE_TIME", beginDate, endDate);
                  stms.append("AND", "c.APP_ID", "=", appId);
                  stms.append("AND", "c.CHANNEL", "=", channel);
		          stms.addGroupBy(" appid,channel,cdate");
		          stms.addOrderBy(" cdate desc");
                  templateDao.search(stms, page);
	}
	
	public void searchIosDailyBak(Page page,String appId,String channel,Date beginDate,Date endDate){
		Statement stms = stmsFactory.createNativeStatement("SELECT a.*,b.c FROM ");
		          stms.append(" (SELECT ic.APP_ID,ic.CHANNEL,DATE_FORMAT(ic.CREATE_TIME,'%Y-%m-%d') d,COUNT(1) cnt FROM IOS_CLICK ic GROUP BY ic.APP_ID,ic.CHANNEL,d) a,");
		          stms.append(" (SELECT ia.APP_ID,ia.CHANNEL,DATE_FORMAT(ia.CREATE_TIME,'%Y-%m-%d') d,COUNT(1) c FROM IOS_CLICK ia WHERE ia.STATUS='1' GROUP BY ia.APP_ID,ia.CHANNEL,d) b ");
		          stms.append(" WHERE a.APP_ID=b.APP_ID AND a.CHANNEL=b.CHANNEL AND a.d=b.d");
                  stms.append("AND", "a.APP_ID", "=", appId);
                  stms.append("AND", "a.CHANNEL", "=", channel);
                  stms.appendBetween("AND", "a.d", beginDate, endDate);
                  stms.addOrderBy("a.d DESC,a.cnt DESC");
                  templateDao.search(stms, page);
	}

}
